﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ClassLibrary.App_Code;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// Summary description for UserDAL
/// </summary>
public class UserDAL
{
	public UserDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static ShopMobileDataContext data = new ShopMobileDataContext();
    // Method Encryp password
    public static string EncryptMD5(string data)
    {
        MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
        byte[] b = System.Text.Encoding.UTF8.GetBytes(data);
        b = MD5.ComputeHash(b);
        StringBuilder s = new StringBuilder();
        foreach (byte p in b)
        {
            s.Append(p.ToString("x").ToLower());
        }
        return s.ToString();
    }
    public static bool checkLogin(string username, string password)
    {
        var check = from us in data.Users
                    where  us.username.ToLower() == username.ToLower() && us.password == password
                    select us;
        if (check.Any())
            return true;
        else
            return false;
    }

    public static bool checkExistUser(string username)
    {
        var check = from us in data.Users
                    where us.username.ToLower() == username.ToLower() 
                    select us.note;
        if (check.Any())
            return true;
        else
            return false;
    }
    public static bool checkExistEmail(string email)
    {
        var check = from us in data.Users
                    where us.email.ToLower() == email.ToLower()
                    select us.note;
        if (check.Any())
            return true;
        else
            return false;
    }
    //check pass||email
    public static bool checkRequestPassword(string user,string email)
    {
        var check = from us in data.Users
                    where us.username.ToLower()==user.ToLower() && us.email.ToLower() == email.ToLower()
                    select us.note;
        if (check.Any())
            return true;
        else
            return false;
    }
    //
    public static bool signupUser(string username, string password, string name, string email, string phone, string address)
    {
        User user = new User();
        user.username = username;
        user.password = EncryptMD5(password);
        user.name = name;
        user.email = email;
        user.address = address;
        user.phone = phone;
        user.note = 0;
        data.Users.InsertOnSubmit(user);
        data.SubmitChanges();
        return true;
    }

    public static UserBLL getUserInfo(string username)
    {
        var info = from u in data.Users
                   where u.username.ToLower() == username.ToLower()
                   select new UserBLL(u.id,u.username,u.password,u.name, u.phone, u.address, u.email);
        return info.First();
    }
    public static bool changeUserInfo(UserBLL us)
    {
        User user = data.Users.Single(u => u.username == us.username);
        user.name = us.name;
        user.phone = us.phone;
        user.address = us.address;
        data.SubmitChanges();
        return true;
    }
    public static bool changePassword(UserBLL us, string password)
    {
        User user = data.Users.Single(u => u.username == us.username);
        if (user.password == EncryptMD5(password))
        {
            user.password =EncryptMD5(us.password);
            data.SubmitChanges();
            return true;
        }
        else
        {
            return false;
        }
    }
    public static bool updatePassword(UserBLL us)
    {
        User user = data.Users.Single(u => u.username == us.username);
        if (EncryptMD5(user.email) == us.email)
        {
            user.password = EncryptMD5(us.password);
            data.SubmitChanges();
            return true;
        }
        else
        {
            return false;
        }
    }


    public static int totalCus()
    {
        var car = from c in data.Users

                  select c.id;
        return car.Count();
    }
}